//可否下拉
var is_bool=true;
// 页码信息
var page_no=1;
//搜索条件
var code='';
var series = '';
var type = '';
// 下拉框对象
var hgs1 = {};
var hgs2 = {};

$(function() {
    getProductList()
    $('#icon-remove-series').click(function() {
    	series = '';
        $('.product').html('');
        page_no=1;
        $('#series').html('产品系列');
        getProductList();
    });
    $('#icon-remove-type').click(function() {
    	type = '';
        $('.product').html('');
        page_no=1;
        $('#type').html('品类名称');
        getProductList();
    });
    $('.icon-remove-code').click(function(){
        code='';
        $('.product').html('');
        page_no=1;
        $(this).prev().val('');
        getProductList();
    });

    $('.code').blur(function(){
        code=$(this).val();
    });

    $('.search-button').click(function(){
        page_no=1;
        $('.product').html('');
        getProductList();
    });
    $('#series').click(function() {
        chooseSeriesParam();
    });
    $('#type').click(function() {
        chooseTypeParam();
    });


    // $(window).scroll(function(){
    // 　　var scrollTop = $(this).scrollTop();
    // 　　var scrollHeight = $(document).height();
    // 　　var windowHeight = $(this).height();
    // 　　if(scrollTop + windowHeight == scrollHeight&&is_bool){
    // 　　　　getProductList();
    // 　　}
    // });
    
    $('.choose-product-content').on('scroll', function() {
        var nDivHight = $(this).height();
        var nScrollHight = $(this)[0].scrollHeight;
        var nScrollTop = $(this)[0].scrollTop;
        if (nScrollTop + nDivHight >= nScrollHight&&is_bool) {
            getProductList();
        }
    })


    $('.cancel').click(function(){
        $('.product-frame iframe', window.parent.document).animate({top:'100%'},500,function(){
            
        });
        setTimeout(function(){
            $('.product-frame', window.parent.document).hide();
        },499);
    });
    $('.confirm').click(function(){
        if($('.radio-choose').length==0){
            return;
        }else{
            $('.radio-choose').each(function(){
                var id=$(this).attr('data-id');
                var product_num=$(this).parents('.product-item').find('.product-num').html();
                var product_img=$(this).parents('.product-item').find('img').attr('src');
                if($('.product-box .openShop-'+id, window.parent.document).length>0){
                    $('.product-box .openShop-'+id, window.parent.document).find('.inputJ').val(Number($('.product-box .openShop-'+id, window.parent.document).find('.inputJ').val())+1);
                }
                else{
                    $('.openShop-demo .shopId span', window.parent.document).html(product_num);
                    $('.openShop-demo img', window.parent.document).attr('src',product_img);
                    $('.openShop-demo .product_id', window.parent.document).val(id);
                    $('.product-box',window.parent.document).append($('.openShop-demo', window.parent.document).html());
                    $('.product-box .openShop:last', window.parent.document).addClass('openShop-'+id); 
                }
            })
            $('.radio').attr('data-choose',0).removeClass('radio-choose');
            $('.cancel').click();
        }
    });
})



function getProductList() {
    //禁止滚动
    is_bool=false;
    $('.product').show();
    //搜索结果
    $('.search-result lable').eq(0).html('?');
    var search_array=[];
    if(code!=''){
        search_array.push(code);
    }
    if(series!=''){
        search_array.push(series);
    }
    if(type!=''){
        search_array.push(type);
    }
    $('.search-result lable').eq(1).html(search_array.join('，'));
    $('.search-result').show();
    $('.load').show();
    $.ajax({
        type: "post",
        url: "Api/Product/getProductList",
        data: {
            park_id: $('#park_id').val(),
            openid:$('#openid').val(),
            code:code,
            series:series,
            type:type,
            page_no:page_no
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                setTimeout(function(){
                    $('.load').hide();
                    var t = JSON.parse(data.v);
                    $('.search-result lable').eq(0).html(t.count);
                    $.each(t.product, function(k, v) {
                        // alert(v.img[0]);
                        $('.product-demo a').attr('href',getRootPath()+'/Home/Product/product?park_id='+$('#park_id').val()+'&product='+v.id);
                        if(v.img[0]){
                           $('.product-demo .product-img img').attr('src',v.img[0]).attr('data-src',v.img[0]); 
                        }
                        else{
                            $('.product-demo .product-img img').attr('src','public/home/images/no-img.jpg').attr('data-src','public/home/images/no-img.jpg'); 
                        }
                        
                        $('.product-demo .product-num').html(sub_str(v.code,7));
                        $('.product-demo .radio').attr('data-id',v.id);
                        $('.product-demo .product-collection lable').html(v.collect_num);
                        if(v.is_collect){
                            $('.product-demo .product-collection i').attr('class','fa fa-heart');
                        }
                        else{
                            $('.product-demo .product-collection i').attr('class','fa fa-heart-o');
                        }   
                        $('.product-demo .product-line-1').html(v.type+'<div style="width:2px; height:16px; background-color:#444; display:inline-block;  position:relative; top:3px;  margin-left:5px; margin-right:5px;"></div>'+v.name);
                        $('.product-demo .product-line-2').html(v.series+'系列');
                        $('.product-demo .product-line-3 lable').html(v.long+'×'+v.wide+'×'+v.high);
                        $('.product').append($('.product-demo').html());
                    });
                    is_bool=t.have;
                    page_no++;
                },1000);
                // lazyLoad();
                // $(window).scroll();
            } else {

            }
        }
    });
}


function chooseSeriesParam() {
    $.ajax({
        type: "post",
        url: "Api/Product/getSeriesData",
        data: {
        	park_id: $('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                hgs1 = new selectSwiper({
                    el: '.select-box1',
                    mustSelect:true, // 是否必选，默认false
                    data: t,
                    init:function(index){},
                    okFunUndefind: function() { //选择空执行
                        errorAlert('请选择');
                        return false;
                    },
                    okFun: function(index) { //确认按钮执行
                        series=this.data[index];
                        page_no=1;
                        $('.product').html('');
                        $('#series').html(series);
                        getProductList();
                    }
                });
                hgs1.openSelectSwiper();
            } else {
            	errorAlert(data.msg);
            }
        }
    });
}

function chooseTypeParam() {
    $.ajax({
        type: "post",
        url: "Api/Product/getTypeData",
        data: {
            park_id: $('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                hgs2 = new selectSwiper({
                    el: '.select-box2',
                    mustSelect:true, // 是否必选，默认false
                    data: t,
                    init:function(index){},
                    okFunUndefind: function() { //选择空执行
                        errorAlert('请选择');
                        return false;
                    },
                    okFun: function(index) { //确认按钮执行
                        type=this.data[index];
                        page_no=1;
                        $('.product').html('');
                        $('#type').html(type);
                        getProductList();
                    }
                });
                hgs2.openSelectSwiper();
            } else {
                errorAlert(data.msg);
            }
        }
    });
}

function chooseId(obj){
    if($(obj).attr('data-choose')=='0'){
        // choose_id.push($(obj).attr('data-id'));
        $(obj).addClass('radio-choose');
        $(obj).attr('data-choose','1');
    }
    else{
        // for (var i =0 ; i <choose_id.length; i++) {
        //     if(choose_id[i]==$(obj).attr('data-id')){
        //         choose_id.splice(i, 1);
        //         break;
        //     }
        // }
        $(obj).removeClass('radio-choose');
        $(obj).attr('data-choose','0');
    }
    // return false;
}

function doChoose(obj){
    if($(obj).find('.radio').attr('data-choose')=='0'){
        // choose_id.push($(obj).attr('data-id'));
        $(obj).find('.radio').addClass('radio-choose');
        $(obj).find('.radio').attr('data-choose','1');
    }
    else{
        // for (var i =0 ; i <choose_id.length; i++) {
        //     if(choose_id[i]==$(obj).attr('data-id')){
        //         choose_id.splice(i, 1);
        //         break;
        //     }
        // }
        $(obj).find('.radio').removeClass('radio-choose');
        $(obj).find('.radio').attr('data-choose','0');
    }
    // $(obj).find('.radio').click();
}